package com.starblink.library.widget.novice_guide.home

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.view.*
import com.skydoves.balloon.BalloonGuide
import com.starblink.android.basic.ext.margin
import com.starblink.library.widget.novice_guide.control.GuidePopStatusControl
import com.starblink.android.basic.ext.toBitmap2LayoutEnhanced
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.basic.util.FlowCountDownTimer
import com.starblink.basic.style.R
import com.starblink.library.widget.databinding.AddVHomeDiscountToastBinding

object HomeDiscountPopView {
    private val downTimer = FlowCountDownTimer(time = 60 * 5)
    private var anchorBitmap: Bitmap? = null

    //埋点只上报一次
    private var show = false

    @SuppressLint("ClickableViewAccessibility")
    fun pushPopWindow(
        anchorView: View,
        parent: ViewGroup,
        dismissCallBack: (() -> Unit)? = null
    ) {
        GuidePopStatusControl.getHollowRectF(anchorView)?.let { rectF ->
            //↓
            val toastLay =
                AddVHomeDiscountToastBinding.inflate(
                    LayoutInflater.from(anchorView.context),
                    parent,
                    false
                )
            toastLay.btm.margin(
                topMargin = rectF.top.toInt()
            )

            //开倒计时的目的是为了和底部的图片加载获得同步
            downTimer.start(callBack = {
                anchorBitmap = anchorView.toBitmap2LayoutEnhanced()
                if (anchorBitmap != null) {
                    toastLay.btm.setImageBitmap(anchorBitmap)
                    if (!show) {
                        show = true
                        var balloon = BalloonGuide.Builder(anchorView.context)
                            .setLayout(toastLay)
                            .setPadding(0)
                            .setOnBalloonDismissListener {
                                dismissCallBack?.invoke()
                                downTimer.cancel()
                            }
                            .setOnBalloonClickListener {
                                SpmTrackHandler.addSingleTrackData(
                                    SkAntEntity(
                                        spmCnt = GTrackerAssistUtils.fetchElementValue(
                                            SpmPageDef.Home080Page_10164,
                                            SpmElementDef.Element_33069
                                        ), params = mapOf("function" to "superdeals")
                                    )
                                )
                            }
                            .setBackgroundColorResource(R.color.sk_gray900_opacity70)
                            .build()
                        if (UserDataCenter.currentIndex != 0) {
                            return@start
                        }
                        balloon.showFull(parent)
                        SpmTrackHandler.addSingleTrackData(
                            SkAntEntity(
                                event = TrackEvent.elementExposure.value,
                                spmCnt = GTrackerAssistUtils.fetchElementValue(
                                    SpmPageDef.Home080Page_10164,
                                    SpmElementDef.Element_33069
                                ), params = mapOf("function" to "superdeals")
                            )
                        )
                    }
                }
            }, onFinish = { })
            //↑

        }

    }
}

